package com.irrigation.controller;

import com.irrigation.dto.SensorDto;
import com.irrigation.service.SensorService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sensors")
@RequiredArgsConstructor
public class SensorController {

    private final SensorService sensorService;

    @GetMapping
    public ResponseEntity<List<SensorDto>> getAllSensors() {
        return ResponseEntity.ok(sensorService.getAllSensors());
    }

    @GetMapping("/{id}")
    public ResponseEntity<SensorDto> getSensorById(@PathVariable Long id) {
        return ResponseEntity.ok(sensorService.getSensorById(id));
    }

    @GetMapping("/device/{deviceId}")
    public ResponseEntity<List<SensorDto>> getSensorsByDeviceId(@PathVariable Long deviceId) {
        return ResponseEntity.ok(sensorService.getSensorsByDeviceId(deviceId));
    }

    @GetMapping("/type/{type}")
    public ResponseEntity<List<SensorDto>> getSensorsByType(@PathVariable String type) {
        return ResponseEntity.ok(sensorService.getSensorsByType(type));
    }

    @PostMapping
    public ResponseEntity<SensorDto> createSensor(@RequestBody SensorDto sensorDto) {
        return ResponseEntity.ok(sensorService.createSensor(sensorDto));
    }

    @PutMapping("/{id}")
    public ResponseEntity<SensorDto> updateSensor(@PathVariable Long id, @RequestBody SensorDto sensorDto) {
        return ResponseEntity.ok(sensorService.updateSensor(id, sensorDto));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteSensor(@PathVariable Long id) {
        sensorService.deleteSensor(id);
        return ResponseEntity.noContent().build();
    }

    @PatchMapping("/{id}/reading")
    public ResponseEntity<SensorDto> updateSensorReading(
            @PathVariable Long id, 
            @RequestParam double value) {
        return ResponseEntity.ok(sensorService.updateSensorReading(id, value));
    }
}
